package im.oen.gui.tools.support.mysql;

import im.oen.gui.tools.data.connect.MysqlConnectData;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Kevin.Xu
 */
public class Connection {

    java.sql.Connection conn = null;

    private MysqlConnectData data;
    private boolean status = true;

    public Connection(MysqlConnectData connectData) {
        this.data = connectData;
        init();
    }

    public boolean update(String sql) {
        this.status = true;
        try {
            if (conn == null || conn.isClosed()) {
                init();
            }
            if (!conn.isClosed()) {
                int result = 0;
                try {
                    Statement statement = conn.createStatement();
                    result = statement.executeUpdate(sql);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                this.status = false;
                return result > 0;
            }
        } catch (Exception e) {
            e.printStackTrace();
            close();
        } finally {
            this.status = false;
        }
        return false;
    }

    public List<Object[]> select(String sql) {
        this.status = true;
        try {
            if (conn == null || conn.isClosed()) {
                init();
            }
            if (!conn.isClosed()) {
                ResultSet rs = null;
                List<Object[]> result = null;
                try {
                    Statement statement = conn.createStatement();
                    rs = statement.executeQuery(sql);
                    result = new ArrayList<Object[]>();
                    while (rs != null && rs.next()) {
                        ResultSetMetaData metaData = rs.getMetaData();
                        int columnCount = metaData.getColumnCount();
                        Object[] objs = new Object[columnCount];
                        for (int i = 0; i < columnCount; i++) {
                            objs[i] = rs.getObject(i + 1);
                        }
                        result.add(objs);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (rs != null) {
                    rs.close();
                }
                this.status = false;
                return result;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            close();
        } finally {
            this.status = false;
        }
        return null;
    }

    private void init() {
        try {
            String url = String.format("jdbc:mysql://%s:%s/gzh_support?useUnicode=true&autoReconnect=true&failOverReadOnly=false&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC&allowPublicKeyRetrieval=true", data.getIp(), data.getPort());
            Class.forName(data.getDriver());
            conn = DriverManager.getConnection(url, data.getUser(), data.getPass());
        } catch (ClassNotFoundException e) {
            close();
        } catch (SQLException e) {
            close();
        }
    }

    private void close() {
        try {
            if (conn != null) {
                conn.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public boolean isStatus() {
        return status;
    }
}
